//
//  WaterEquipListViewController.m
//  OutesIOS
//
//  Created by norman on 2019/1/13.
//  Copyright © 2019 norman. All rights reserved.
//

#import "WaterEquipListViewController.h"
#import "HttpTool.h"

@interface WaterEquipListViewController ()

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *dataSource;
@property (nonatomic,assign) double total_load;

@end

@implementation WaterEquipListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"选择设备";
    [self initData];
    [self setupViews];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

/**
 *  初始化数据
 */
- (void)initData {
    self.dataSource = [NSMutableArray new];
}

#pragma mark - 初始化视图
- (void)setupViews{
    if (self.machineSelectedBlock != nil) {
        //自定义完成选择按钮
//        UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成选择" style:UIBarButtonItemStyleDone target:self action:@selector(onSave)];
//        self.navigationItem.rightBarButtonItem = saveBtn;
        
        //自定义返回按钮
        UIBarButtonItem *returnBtn = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(onReturn)];
        self.navigationItem.leftBarButtonItem = returnBtn;
    }
    self.tableView = [[UITableView alloc] initWithFrame:[UIView frameWithOutNav] style:UITableViewStylePlain];
    self.tableView.backgroundColor = CM_TABLE_BG_COLOR;
    [self.tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)loadData {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:kHeatProject.heat_project_id forKey:@"heat_project_id"];
    [param setValue:self.building_id forKey:@"building_id"];
    [param setValue:[AccountManager manager].token forKey:@"token"];
    [HttpTool get:HTTP_GET_HEAT_PROJECT_BUILDING_EQUIP_LIST params:param success:^(NSDictionary *responseObj) {
        [self.tableView.mj_header endRefreshing];
        self.dataSource = [responseObj objectForKey:@"equips"];
        self.total_load = [[responseObj objectForKey:@"total_load"] doubleValue];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

#pragma mark - 事件响应
- (void)onReturn {
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"XLCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    NSDictionary *item = [self.dataSource objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"【%@】%@", [item objectForKey:@"Series"], [item objectForKey:@"Model"]];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"    X %@台", [item objectForKey:@"count"]];
    if (![Tools isEmpty:self.selectedGuid] && [self.selectedGuid isEqualToString:[item objectForKey:@"Model"]]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *item = [self.dataSource objectAtIndex:indexPath.row];
    if (![Tools isEmpty:self.selectedGuid] && [self.selectedGuid isEqualToString:[item objectForKey:@"Id"]]) {
        self.selectedGuid = @"";
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    } else {
        if (self.machineSelectedBlock) {
            self.machineSelectedBlock(item, self.total_load);
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, CGFLOAT_MIN)];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, CGFLOAT_MIN)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
